package Gui;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.JSplitPane;
import java.awt.Component;
import javax.swing.Box;
import controller.CreateListControl;
import controller.HistoryControl;
import controller.ReportControl;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import java.awt.Canvas;


// TODO: Auto-generated Javadoc
/**
 * The Class CreateReport.
 */
public class CreateReport extends JPanel{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6400283674228776823L;
	
	/** The confirmation lable. */
	public static JLabel confirmationLable=null;
	
	/** The lst. */
	private CreateListControl lst;
	
	/** The ch. */
	private HistoryControl ch=null;
	
	/** The rc. */
	private ReportControl rc=null;
	
	/** The panel. */
	static JPanel  panel = null;
	
	/** The btn new button. */
	public static JButton  btnNewButton = null;
	
	/** The btn new button_1. */
	public static JButton  btnNewButton_1 = null;
	
	/** The btn new button_2. */
	public static JButton  btnNewButton_2 = new JButton("main menu");;
	
	/** The btn enter. */
	public static JButton btnEnter;
	
	/** The btn create. */
	public static JButton btnCreate;
	
	/** The lbl pick a report. */
	public static JLabel lblPickAReport=null;
	
	/** The lbl enter deal. */
	public static JLabel lblEnterDeal=null;
	
	/** The lbl new label. */
	public static JLabel lblNewLabel=null;
	
	/** The btn create report. */
	public static JButton btnCreateReport =null; 
	
	/** The text field. */
	static JTextField textField;
	
	/** The text area_1. */
	static JTextArea textArea_1;
	
	/** The text area. */
	static JTextArea textArea;
	
	/** The table. */
	JTable table;
	
	/** The reportdeal. */
	public String[] reportdeal=new String[10];
	
	/** The scroll pane_1. */
	JScrollPane scrollPane_1;
	
	/** The lbl create report. */
	static JLabel lblCreateReport;
	
	/** The lbl deal report. */
	static JLabel lblDealReport;
	
	/** The panel_1. */
	JPanel panel_1;
	
	/** The table_1. */
	private JTable table_1;
	
	/** The scroll pane_2. */
	private JScrollPane scrollPane_2;
	
	/** The avgpur. */
	public int avgpur;
	
	/** The avgdec. */
	public int avgdec;
	
	/** The amount. */
	public int amount;
	
	/** The btn create report_1. */
	JButton btnCreateReport_1;
	
	/** The scroll pane_3. */
	JScrollPane scrollPane_3;
	
	/** The scroll pane. */
	JScrollPane scrollPane;
	public String path=null;
;
	
	/**
	 * Instantiates a new creates the report.
	 */
	public CreateReport()
	{
		super();
		 viewReport();
	}

	/**
	 * View report.
	 */
	public void viewReport(){
		MarketingDirectorGui.WelcomeLabel.setVisible(false);
		this.setSize( 1500,765 );
		this.setBackground(SystemColor.activeCaption); 
		this.setLayout(null); //set layout
		ch=new HistoryControl();
		rc=new ReportControl();
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(56, 244, 531, 56);
		add(scrollPane);
		
		panel = new JPanel();
		scrollPane.setViewportView(panel);
		
		 btnNewButton = new JButton("Deal Report");
		 btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnEnter.setVisible(true);
				lblEnterDeal.setVisible(true);
				textField.setVisible(true);
				textField.setText("");
				textArea_1.setVisible(true);
				 lblDealReport.setVisible(true);
				 scrollPane_1.setVisible(false);
			}
		});
		btnNewButton.setBounds(431, 60, 113, 23);
		add(btnNewButton);
		
		btnNewButton_2.setBounds(1037, 33, 120, 23);
		add(btnNewButton_2);
		
		lblPickAReport = new JLabel("Pick a report you wish to create:");
		lblPickAReport.setFont(new Font("Arial", Font.BOLD, 18));
		lblPickAReport.setBounds(405, 35, 330, 14);	
		add(lblPickAReport);
		
		 lblEnterDeal = new JLabel("Enter deal:");
		 lblEnterDeal.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblEnterDeal.setBounds(67, 163, 120, 14);
		add(lblEnterDeal);
		
		textField = new JTextField();
		textField.setBounds(164, 162, 86, 20);
		add(textField);
		
		textField.setColumns(10);
		 btnEnter = new JButton("Check Deal");
		 btnEnter.setFont(new Font("Tahoma", Font.BOLD, 12));
		 btnEnter.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent arg0) {
		 		
		 		lst=new CreateListControl(textField.getText());
				 if(!lst.checkdetail()) //check if the field empty
				 {
					 confirmationLable.setVisible(true);
					 confirmationLable.setText("Please fill all the fields!!");
				 }
				 else 
				 { 
					 
					 lst.submitCreateList();
					 btnCreate.setVisible(true);
					 confirmationLable.setText("");
					 rc.getdealhistory(textField.getText());
					lblCreateReport.setVisible(true);
					 lblDealReport.setVisible(true);
					
					 }
		 	}
		 });
		btnEnter.setBounds(260, 161, 151, 23);
		add(btnEnter);
		
		btnCreate = new JButton("Create");
		btnCreate.setFont(new Font("Tahoma", Font.BOLD, 12));
		 btnCreate.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent arg0) {
				panel.setVisible(true);
				 textArea.setVisible(true);
				 scrollPane_1.setVisible(true);
				 scrollPane.setVisible(true);
				//--------------------------------------
		 	
				 String mainreport=textArea_1.getText();
		 		 //-----------------------------------
		 		 String[] rejection=new String[6];
		 		 rejection=mainreport.split("1: ");
		 		reportdeal[0]=rejection[1];
		 		reportdeal[1]=rejection[2];
		 		reportdeal[2]=rejection[3];
		 		//------------------------------------
		 		table = new JTable();
				scrollPane.setViewportView(table);
				table.setModel(new DefaultTableModel(
					new Object[][] {
						{reportdeal[0], reportdeal[2], reportdeal[1]},
					},
					new String[] {
						"number of customers", "time spend on calls", "sale events"
					}
				));
		 		textArea.setText(rejection[0]);
		 		path= rc.Create_DealReport(reportdeal,rejection[0],textField.getText());
		 		JOptionPane.showMessageDialog(null,"The file was created.\n"+path);
		 	}
		 });
		btnCreate.setBounds(260, 197, 89, 23);
		add(btnCreate);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(56, 312, 427, 72);
		add(scrollPane_1);
		
		textArea = new JTextArea();
		scrollPane_1.setRowHeaderView(textArea);
		textArea.setEditable(false);
		
	    textArea_1 = new JTextArea();
		textArea_1.setBackground(SystemColor.activeCaption);
		textArea_1.setBounds(446, 377, 195, 174);
		
		
		confirmationLable = new JLabel("\n");   //set label for result
		confirmationLable.setForeground(new Color(204, 0, 51));
		confirmationLable.setFont(new Font("Tahoma", Font.BOLD, 14));
		confirmationLable.setBounds(405, 609, 589, 41);  //set label size
		add(confirmationLable);
		
		lblCreateReport = new JLabel("Press to Create Report:");
		lblCreateReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblCreateReport.setBounds(67, 197, 170, 19);
		add(lblCreateReport);
		
		lblDealReport = new JLabel("Deal Report");
		lblDealReport.setForeground(new Color(165, 42, 42));
		lblDealReport.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 22));
		lblDealReport.setBounds(143, 90, 175, 41);
		add(lblDealReport);
		
		lblNewLabel = new JLabel("Customer Report");
		lblNewLabel.setForeground(new Color(165, 42, 42));
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 22));
		lblNewLabel.setBounds(787, 99, 294, 32);
		add(lblNewLabel);
		
		scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(674, 244, 487, 140);
		add(scrollPane_3);
		
		panel_1 = new JPanel();
		scrollPane_3.setViewportView(panel_1);
	/**
	 * Customer Report
	 */
		 btnNewButton_1 = new JButton("Customer");
		 btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblNewLabel.setVisible(true);
				btnCreateReport.setVisible(true);
				textArea_1.setVisible(true);
			rc.Create_CustomerReport();	
				 
			}
		});
		
		btnNewButton_1.setBounds(554, 60, 114, 23);
		add(btnNewButton_1);
		/**
		 * Create report table
		 */
		btnCreateReport = new JButton("Create Report");
		btnCreateReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnCreateReport_1.setVisible(true);
				panel_1.setVisible(true);
				scrollPane_3.setVisible(true);
				//--------------------------------------
				 String mainreport=textArea_1.getText();
		 		 //-----------------------------------
		 		 String[] Customers=new String[200];
		 		 
		 		String[] calc1=new String[2];
				calc1=mainreport.split(" purchase: "); 
				
				String[] calc2=new String[2];
				calc2=calc1[1].split(" decline: ");
				
				String[] calc3=new String[2];
				calc3=calc2[1].split(" amount: ");
		 		//------------------------------------
				int n=Integer.parseInt(calc3[1]);
				Object[][] report= new Object[n][3];
				Customers=calc1[0].split("-");
				
				report=rc.create_chart(Customers);
				
				 table_1 = new JTable();
					scrollPane_3.setViewportView(table_1);
					table_1.setFillsViewportHeight(true);
					table_1.setModel(new DefaultTableModel(report,new String[] {
							"Customer Id", "Customer name", "Percent of purchases"}
					));
					amount=Integer.parseInt(calc3[1]);
					
			}
			
		});	
		btnCreateReport.setBounds(841, 161, 140, 23);
		add(btnCreateReport);	
		
		btnCreateReport_1 = new JButton("Create Excel file");
		btnCreateReport_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				path=rc.CustomerReport_file(amount);
	 		JOptionPane.showMessageDialog(null,path);
			}
		});
		btnCreateReport_1.setBounds(730, 423, 170, 23);
		add(btnCreateReport_1);
		
	
		}
	}
